package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.glory.brepda.R;
import com.glory.brepda.adapter.AddCarrierDataAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.utils.LotUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TestLibTrackInActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvLotStates)
    TextView tvLotStates;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.etCarrier)
    FreeUI_ClearEditText etCarrier;
    @BindView(R.id.rvCarrierList)
    RecyclerView rvCarrierList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private AddCarrierDataAdapter carrierAdapter;
    private String eqpID;
    private List<String> mDurablelist;
    private String woid;
    private LotInfoEntity lotInfo;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_test_lib_track_in;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        initRecyclerView(rvCarrierList,this);
        carrierAdapter = getCarrierAdapter();
        rvCarrierList.setAdapter(carrierAdapter);
        initEdit(etLot, s -> {
            //获取lot信息
            LotUtils.searchLotInfo(TestLibTrackInActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    lotInfo = response;
                    Constant.LIMITPACKAGETYPEQTY = response.getLIMITPACKAGETYPEQTY();
                    Constant.PACKAGETYPE = response.getPACKAGETYPE();
                    Constant.WIRESPEC = response.getWIRESPEC();
                    Constant.BDCODE = response.getBD_CODE();
                    Constant.PARTNAME = response.getLOT().getPARTNAME();
                    woid = response.getLOT().getWOID();
                    tvStep.setText(response.getLOT().getSTEPNAME() + "/" + response.getLOT().getSTEPDESC());
                    tvLotStates.setText(response.getLOT().getSTATE());
                    eqpID = response.getLOT().getEQUIPMENTID();
                    mDurablelist = response.getDURABLELIST();
                    carrierAdapter.setNewData(mDurablelist);
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t,"LOTQUERY");
                }
            });
        });

        initEdit(etCarrier, s -> {
            if (mDurablelist == null){
                showToast("请先扫描批次号!", false);
                return;
            }


            if (carrierAdapter.getData().contains(s)) {
                showToast("当前载具已添加到载具列表中", false);
                return;
            }

            if (mDurablelist.contains(s)){
                carrierAdapter.getData().add(s);
                carrierAdapter.notifyDataSetChanged();
            }else {
                showToast("当前载具不匹配!", false);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
//        if (actionControl("Submit20")){
//            return;
//        }

        if (getText(etLot).equals("")){
            showToast("请扫描批次号!",false);
            return;
        }

        if (tvLotStates.getText().toString().equals("待投产")){
            showConfirmDialog("批次未投产，请联系计划部执行投产");
            return;
        }

        if (tvLotStates.getText().toString().equals("正在作业")){
            showConfirmDialog("批次正在作业，请直接操作出站功能");
            return;
        }

        //actionType NoEqpTrackIn DefaultTrackIn

        LotUtils.lotTrackIn(getSupportFragmentManager(),lotInfo,null,woid,"LOTTRACKIN",this,eqpID,getText(etLot),carrierAdapter.getData(),"NoEqpTrackIn");
    }

}
